#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sqlite3.h>
#include"parse.h"
#include"dataoperation.h"

static int callback(void *un_used,int argc, char* argv[], char* azColName[]){
	int i;
	//fprintf(stderr,"%s: ", (const char*)data);
		for(i = 0; i<argc;i++){
			printf("%s = %s\n",azColName[i],argv[i] ? argv[i] : "NULL");
		}
		printf("\n");
		return 0;
}

int main(int argc, char* argv[]){
	sqlite3 *db;
	char *zErrMsg = 0;
	int rc;
	char* table;
	char *insert;
	char* sel;
	char* param;
	char* update;
	char* delete;
	int choice;
	char arg[24];
	
		if(argc < 2){
			fprintf(stderr,"Usage: %s DATABASE SQL-SATEMENT\n",argv[0]);
			return(1);
		}
		rc = sqlite3_open(argv[1],&db);
			if(rc){
				fprintf(stderr,"Can't open database: %s\n",sqlite3_errmsg(db));
				sqlite3_close(db);
				return(1);
			}else{
				fprintf(stderr,"Opened database successfully\n");
			}
			//simple commandline gui
			gui();
			
			scanf("%d",&choice);
				if(choice == 1){
			//Function calls
			table = create_table();
			param = table;
			}	if(choice == 2){
				//insert = insert_data();
				insert = collect_user_data();
				param = insert;
			}
				if(choice == 3){
					printf("Give search argument\n");
					printf("* prints all data\n");
					printf("or give name\n");
					scanf("%s",arg);
					sel = select_data(arg);
					param = sel;
				}
				if(choice == 4){
					update = collect_update_data();
					param = update;
				}
				if(choice == 5){
					delete = collect_delete_data();
					param = delete;
				}
			
         //execute command
		rc = sqlite3_exec(db,param,callback,0,&zErrMsg);
			if(rc !=SQLITE_OK){
				fprintf(stderr,"SQL error: %s\n",zErrMsg);
				sqlite3_free(zErrMsg);
			}else{
				fprintf(stderr,"Operation done successfully\n");
			}
			param ="";
			sqlite3_close(db);
	
	return 0;
}
